//
//  OAuthViewController.swift
//  Weibo
//
//  Created by 明丽 on 16/1/13.
//  Copyright © 2016年 明丽. All rights reserved.
//

import UIKit

import SVProgressHUD
import AFNetworking



class OAuthViewController :UIViewController {
//设置属性
    let webView = UIWebView()
    @objc private func close(){
 
        //let v = MKVViewController()
    dismissViewControllerAnimated(true, completion: nil)
    
        
    }
    
   
    //自动填充账号
  @objc private  func defaultAccount (){
    let jsString = "document.getElementById('userId').value = '18911930702', document.getElementById('passwd').value = 'huihuang201314..' "
    
    //oc 调用js方法
    webView.stringByEvaluatingJavaScriptFromString(jsString)
    
    
    }
    
    
    override func loadView() {
        //替换视图.
        view = webView
        //设置代理
        webView.delegate = self
    }
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        setNavBar()
        loadOAuthPage()
    }

    
    private func setNavBar(){
    
    self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "关闭", style: .Plain, target: self, action: "close")
        
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "填充", style: .Plain, target: self, action: "defaultAccount")
    
    }
    //加载授权页面
    func loadOAuthPage(){
        let urlSTring = "https://api.weibo.com/oauth2/authorize?client_id=3175735468&redirect_uri=https://www.taobao.com"
        let url = NSURL(string: urlSTring)!
        let request = NSURLRequest(URL: url)
        webView.loadRequest(request)
    }
}
    //类扩展extension代表扩展的意思
    extension OAuthViewController:UIWebViewDelegate{
    //当webVIEW还没完全加载出来会调用
        //显示 用户等待指示器
//        func webViewDidStartLoad(webView: UIWebView) {
//            //显示
//            SVProgressHUD.show()
//        }
//        
//        func webViewDidFinishLoad(webView: UIWebView) {
//            //隐藏
//            SVProgressHUD.dismiss()
//        }

     //返回yes就是可以返回在网络中加载的数据.no就是不加载
        func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool{
        
            guard let userString = request.URL?.absoluteString else{
            
            return false
            }
            if userString.containsString("code="){
                guard let query = request.URL?.query else{
                
                return false
                }
                let codestr = "code="
                let code = (query as NSString).substringFromIndex(codestr.characters.count)
                print(code,query)
                loadAccessToken(code)

                
                return false
            }
            
            
            return true
        }
//        

        
         //        //在webview还没完全之前调用
        func webViewDidStartLoad(webView: UIWebView)
        {
            SVProgressHUD.show()
        
        }
        //在webview完全显示的时候调用
        func webViewDidFinishLoad(webView: UIWebView){
        SVProgressHUD.dismiss()
        }
        
        
        private func loadAccessToken(code: String){
            UserAccountViewController().loadAccessToken(code) { (isSuccess) -> () in
                if !isSuccess{
                
                print("用户登录失败")
                    SVProgressHUD.showErrorWithStatus("网络不好,请稍后")
                    return
                }
                SVProgressHUD.showSuccessWithStatus("登陆成功")
                self.dismissViewControllerAnimated(false, completion:{
                    () -> Void in
                    //发出通知
                    NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootVC, object: "Welcome")
                    
                    })
                    
                
//                //发出通知
//            NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootVC, object: "Welcome")
            }
        }

        
}

